package com.smartview.jboss.spring.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import org.apache.log4j.Logger;
import org.springframework.util.StopWatch;

public class PMInterceptor implements MethodInterceptor {
	
	public Object invoke( MethodInvocation invocation ) 
		throws Throwable {
		
		String className = invocation.getThis().getClass().getName();
		String methodName = className +  "." 
			+ invocation.getMethod().getName() + "()"; 
		StopWatch sw = new StopWatch( methodName );
		
		if( Logger.getRootLogger().isDebugEnabled() ) {
			Logger.getRootLogger().debug( methodName + " start" );
			Object[] args = invocation.getArguments();
			if( (args != null) && ( args.length >  0 )) {
				for( int idx = 0; idx < args.length; idx++ ) {
					Logger.getRootLogger().debug("Argument[" + idx +"] : " 
							+ args[idx]);
				}
			}	
			sw.start(methodName);
		}
		
		Object retVal = invocation.proceed();
		
		if( Logger.getRootLogger().isDebugEnabled() ) {
			sw.stop();
			Logger.getRootLogger().debug( sw.shortSummary() );
			Logger.getRootLogger().debug( methodName + " end" );
		}
		 
		return retVal;
	}
	
}
